/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SecretSanta;

import java.util.ArrayList;
import java.util.List;
import SecretSanta.core.*;

/**
 *
 * @author mrandell
 */
public class SecretSanta 
{
    SecretSantaCore core;
    List<Person> participants;
    HistoryMap last_history;
    boolean restrict_last_years;
    
    SecretSanta()
    {
        core = new SecretSantaCore();
        last_history = core.getLatestHistory();
        participants = core.getPeople(last_history.getAllSantaNames());
        restrict_last_years = true;
        //Remove 
    }
    
    public List<String> getParticipantNames()
    {
        if (participants == null)
        {
            return null;
        }
        ArrayList<String> names = new ArrayList();
        for(int i =0, j = participants.size(); i < j; i++)
        {
            names.add(participants.get(i).getName());
        }
        return names;
    }
    
    public String getParticipantEmail(String name)
    {
        Person target = this.findParticipant(name);
        if(target == null)
        {
            return null;
        }
        
        return target.getEmail();
    }
    
    public  List<String> getParticipantRestrictions(String name)
    {
        Person target = this.findParticipant(name);
        if(target == null)
        {
            return null;
        }
        
        return target.getRestrictions();
    }
    
    public String getParticipantLastMatch(String name)
    {
        return last_history.getReciever(name);
    }
    
    public void addParticipant(String name, String email)
    {
        participants.add(new Person(name, email));
    }
    
    public void addParticipant(String name, String email, List<String> restrictions)
    {
        participants.add(new Person(name, email, restrictions));
    }
    
    public void removeParticipant(String name)
    {
        Person target = this.findParticipant(name);
        if(target == null)
        {
            return;
        }
        participants.remove(target);
    }
    
    public void removeAllParticipants()
    {
        participants.clear();
    }
    
    public boolean modifyParticipantName(String name, String new_name)
    {
        Person target = this.findParticipant(name);
        if(target == null)
        {
            return false;
        }
        
        target.setName(new_name);
        return true;
    }
    
    public boolean modifyParticipantEmail(String name, String new_email)
    {
        Person target = this.findParticipant(name);
        if(target == null)
        {
            return false;
        }
        
        target.setEmail(new_email);
        return true;
    }
    
    public boolean modifyParticipantRestrictions(String name, List<String> new_restrictions)
    {
        Person target = this.findParticipant(name);
        if(target == null)
        {
             return false;
        }
        for(int i = 0,j = new_restrictions.size(); i < j; i++)
        {
            if(this.findParticipant(new_restrictions.get(i)) == null)
            {
                return false;
            }
        }
        target.setRestrictions(new_restrictions);
        return true;
    }
    
    public boolean match()
    {
        return core.generateMapping(participants, restrict_last_years);
    }
    
    public boolean matchAndSend()
    {
        return core.generateMappingAndSend(participants, restrict_last_years);
    }
    
    public boolean sendAllEmails()
    {
        return core.sendEmails();
    }
    
    public boolean sendEmail(String name)
    {
        return core.sendEmail(name);
    }
        
    //PRIVATE
    
    private Person findParticipant(String name)
    {
        for(int i =0, j = participants.size(); i < j; i++)
        {
            if(participants.get(i).getName().equals(name))
            {
                return participants.get(i);
            }
        }
        return null;
    }
    
    
}
